package com.alinesno.cloud.base.logger.alarm.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alinesno.cloud.base.logger.alarm.AlarmService;
import com.alinesno.cloud.base.logger.alarm.constants.AlarmTypeConstants;
import com.alinesno.cloud.base.logger.entity.LogMethodRuntimeEntity;
import com.alinesno.cloud.base.logger.entity.LogTriggerConditionEntity;
import com.alinesno.cloud.base.logger.entity.LogTriggerEntity;
import com.alinesno.cloud.base.logger.service.ILogMethodRuntimeService;
import com.alinesno.cloud.base.notice.entity.EmailSendEntity;
import com.alinesno.cloud.base.notice.entity.SmsSendEntity;
import com.alinesno.cloud.common.facade.wrapper.RestWrapper;

/**
 * 邮件通知
 * 
 * @author WeiXiaoJin
 * @since 2020年1月29日 下午19:45:08
 */
@Component(AlarmTypeConstants.ALARM_METHOD_RUNTIME + AlarmTypeConstants.SERVICE_SUFFIX)
public class MethodRuntimeAlarmServiceImpl extends AlarmService {

	@Autowired
	private ILogMethodRuntimeService methodRuntionService;

	
	private List<LogMethodRuntimeEntity> es = null;
	
	@Override
	public boolean doAlarm(LogTriggerEntity e, List<LogTriggerConditionEntity> cList) {
		RestWrapper restWrapper = buildFilterCondition(cList) ; 
		es = methodRuntionService.findAll(restWrapper);
		cs = cList ; 

		return es.size() > 0 ? true : false;
	}

	@Override
	protected EmailSendEntity generatorEmail(LogTriggerEntity e) {
	
		if(StringUtils.isBlank(e.getNoticeEmail())) {
			log.warn(e.getTriggerName() + " 预警接收邮件接收者为空.");
			return null ; 
		}
		String subTitle = "应用【"+e.getApplicationName()+"】系统异常：" + e.getTriggerName() ; 
		
		StringBuffer sb = new StringBuffer() ; 
		sb.append("<b>预警信息</b>"+NR) ;
		sb.append("应用名:" + e.getApplicationName() + NR) ; 
		
		if(es.size() > EMAIL_ERROR_DEFAULT_LENGTH) {
			sb.append("异常项:" + es.size() +NR) ;
		}
		
		for(int i = 0 ; i < (es.size()>EMAIL_ERROR_DEFAULT_LENGTH?EMAIL_ERROR_DEFAULT_LENGTH:es.size()) ; i ++) {
			LogMethodRuntimeEntity b = es.get(i) ; 
			sb.append("异常类:" + b.getMethodDesc()+":" + b.getMethod() + NR) ;
			sb.append("调用总数:" + b.getCount() + NR) ;
			sb.append("成功:" + b.getOk() + NR) ;
			sb.append("失败:" + b.getFail() + NR) ;
			sb.append("平均时间(ms):" + b.getAvg() + NR) ;
			sb.append("最小时间(ms):" + b.getMin() + NR) ;
			sb.append("最大时间(ms):" + b.getMax() + NR) ;
		}
		
		if(es.size() > EMAIL_ERROR_DEFAULT_LENGTH) {
			sb.append("更新 ...... " +NR) ;
		}
		
		sb.append("<b>监控项</b>"+NR) ;
		for(LogTriggerConditionEntity b : cs) {
			sb.append(b.getConditionName() + ":" + b.getConditionValue() + NR) ;
		}
		
		EmailSendEntity email = new EmailSendEntity() ; 
		email.setEmailSubject(subTitle);
		email.setEmailContent(sb.toString());
		email.setEmailReceiver(e.getNoticeEmail()); 
		
		return email ;
	}

	@Override
	protected SmsSendEntity generatorSms(LogTriggerEntity e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void generatorWechat(LogTriggerEntity e) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void generatorDingtalk(LogTriggerEntity e) {
		// TODO Auto-generated method stub

	}

}
